<?php
/**
 * 收货地址精度类
 * @package  htdocs
 * @subpackage  module/base
 * @author   create by allen.qiang
 * @version 1.0
 */

class addressPrecisions {
    /**
     * 查询会员收货地址
     * @param array $arg_array                          查询参数数组
     * $arg_array = array (
     *       'sortname' => string 排序字段
     *       'sortorder' => string 排序方式
     *       'page' => int 分页页码
     *       'rp' => int 分页页数
     * )
     * @return null
     */

    public function searchCustDeliver( &$arg_array ) {

        global $G_SHOP;

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //会员号
        if (isset($arg_array['pcustID']) && is_numeric($arg_array['pcustID'])) {
            $sql .= ' AND pcustID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['pcustID'];
        }

        //收货地址城市id
        if (isset($arg_array['cityid']) && is_numeric($arg_array['cityid'])) {
            $sql .= ' AND cityid = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['cityid'];
        }

        //收货地址ID
        if (isset($arg_array['deliverID']) && is_numeric($arg_array['deliverID'])) {
            $sql .= ' AND deliverID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['deliverID'];
        }

        //地址
        if (isset($arg_array['address']) && !empty($arg_array['address'])) {
            $sql .= ' AND address LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . encrypt_paramD($arg_array['address']) . '%';
        }

        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 0) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM cust_deliver ' . $sql;
                $count_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM cust_deliver ' . $sql;
            $temp_item_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'pcustID' => $item->pcustID,
                    'deliverID' => $item->deliverID,
                    'provinceName' => $item->provinceName,
                    'cityName' => $item->cityName,
                    'countyName' => $item->countyName,
                    'provinceid' => $item->provinceid,
                    'cityid' => $item->cityid,
                    'countyid' => $item->countyid,
                    'address' => decryptD($item->address),
                    'custName' => decryptD($item->custName),
                    'postalCode'=>$item->postalCode,
                    'mobile' => decryptD($item->mobile),
                    'phone' => decryptD($item->phone),
                    'longitude' => $item->longitude,
                    'latitude' => $item->latitude,
                    'precise' => $item->precise
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 新增/修改地址
     * @param array $arg_array 地址参数数组
     * @return array
     */
    public function editAddress( $arg_array ) {

        global $G_SHOP;

        if (!isset( $arg_array['deliverID'], $arg_array['longitude'], $arg_array['latitude'],$arg_array['precise'],$arg_array['provinceName'],$arg_array['cityName'],$arg_array['countyName'])) {
            return array( 'code' => 400, 'message' => '参数错误' );
        }


        try {

            $addressPrecision = new entity('cust_deliver');

            $addressPrecision->deliverID = $arg_array['deliverID'];
            if (!$G_SHOP->DBCA->fetch($addressPrecision)) {
                throw new Exception('地址信息没有找到', 404);
            }

            $addressPrecision->longitude = $arg_array['longitude'];
            $addressPrecision->latitude = $arg_array['latitude'];
            $addressPrecision->precise = $arg_array['precise'];
            $addressPrecision->provinceName = $arg_array['provinceName'];
            $addressPrecision->cityName = $arg_array['cityName'];
            $addressPrecision->countyName = $arg_array['countyName'];
            $addressPrecision->provinceid = $arg_array['provinceid'];
            $addressPrecision->cityid = $arg_array['cityid'];
            $addressPrecision->countyid = $arg_array['countyid'];

            $G_SHOP->DBCA->save($addressPrecision);
            return array( 'code' => 200, 'message' => '地址修改成功' );

        } catch (Exception $e) {

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }

    }


}